package org.jeecg.modules.tz.controller;

import io.swagger.annotations.Api;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.system.udp.UdpConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.ip.udp.UnicastSendingMessageHandler;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/udp")
@Api(tags = "向嵌入式发送udp数据")
public class UdpClientController {
    private final static Logger logger = LoggerFactory.getLogger(UdpClientController.class);

    @PostMapping("/send")
    public Result send(@RequestParam("message") byte[] message) {
        UnicastSendingMessageHandler handler = new UnicastSendingMessageHandler("localhost", UdpConfig.getSendingPort());
        logger.info("发送UDP信息: {" + message + "}");
        handler.handleMessage(MessageBuilder.withPayload(message).build());
        logger.info("发送成功");
        return new Result<>();
    }
}
